package com.moody.web;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.domain.OrgInfo;
import com.moody.hibernate.domain.RoleInfo;
import com.moody.hibernate.domain.UserInfo;
import com.moody.init.RoleRightInit;
import com.moody.service.SystemService;

/*
 * Copyright © Moody analytics   
 *
 * @Title: UserController.java 
 * @Package： com.moody.web 
 * @author： Fuqiang Wang
 * @Description: 用户管理：管理客户登录以及首页等相关信息的显示
 * @date： 2012-4-16 上午10:42:32
 * @Modified by: Fuqiang Wang 
 * @version V1.0   
 */

@Controller
@RequestMapping("/user")
public class UserController {
	private static final Logger log = LoggerFactory
			.getLogger(UserController.class);
	WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	ServletContext context = web.getServletContext();  
	WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	
	@Resource
	private SystemService systemService;

	@RequestMapping("/login")
	public String login(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		UserInfo user = systemService.findLoginUser(username, password);

		if (user != null) {
			String orgId = user.getBelongorg();
			OrgInfo org = systemService.findOrgById(orgId);
			List<RoleInfo> roleList = systemService.findRoleByUserId(user
					.getUserId());
			RoleInfo role = null ;

			request.getSession().removeAttribute("logonUser");
			request.getSession().setAttribute("logonUser", user);
			
			request.getSession().removeAttribute("password");
			request.getSession().setAttribute("password", user.getPassword());

			if (org != null) {
				request.getSession().removeAttribute("logonUserOrg");
				request.getSession().setAttribute("logonUserOrg", org);
			}

			if (roleList != null && roleList.size() > 0) {
				role = roleList.get(0);
				request.getSession().removeAttribute("logonUserRole");
				request.getSession().setAttribute("logonUserRole", role);
			}
			
			//有权限访问的链接
			if(role != null){
				String accessURL = RoleRightInit.getInstance().getRightMap().get(role.getRoleId()).get("accessURL");
				String deniedURL = RoleRightInit.getInstance().getRightMap().get(role.getRoleId()).get("deniedURL");
				request.getSession().removeAttribute("accessURL");
				request.getSession().setAttribute("accessURL", accessURL);
				request.getSession().removeAttribute("deniedURL");
				request.getSession().setAttribute("deniedURL", deniedURL);
			}
			
			Date now = new Date();
			DateFormat d1 = DateFormat.getDateTimeInstance(); // 默认语言（汉语）下的默认风格（MEDIUM风格，比如：2008-6-16
															// 20:54:53）
			String logonTime = d1.format(now);
			request.getSession().removeAttribute("logonTime");
			request.getSession().setAttribute("logonTime", logonTime);

			return "main";
		} else {
			List<UserInfo> userList = systemService.findLoginUserByUserName(username);
			
			if(userList.size()<=0){
				//账号错误
				request.setAttribute("error", 1);
			}else{
				//账号正确，密码错误
				request.setAttribute("error", 2);
			}
			return "login";
		}
	}
	
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.getSession().removeAttribute("logonUser");
		request.getSession().removeAttribute("logonUserOrg");
		request.getSession().removeAttribute("logonUserRole");
		request.getSession().removeAttribute("logonUserOrg");
		request.getSession().removeAttribute("logonTime");
		
		return "login";
	}
}
